﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;

namespace Rose.Controllers
{
    [Authorize]
    public class FeelingController : Controller
    {
     
        private FeelingHelpModel feelinghelpmodel = new FeelingHelpModel();

      

        [Authorize]
        [HttpPost]
        public ActionResult AddFeeling(string content,string feel)
        {
            try
            {
                string selfId = new CheckHelpModel().CheckCurrentUserId();

                feelinghelpmodel.AddFeelingToDatabase(content, feel, selfId);

                return Content("添加成功");
            }
            catch(Exception ex)
            {
                return Content("错误："+ex.Message);
            }

        }



        [Authorize]
        [HttpPost]
        public ActionResult DeleteFeeling(string modelId)
        {
            try
            {
                string selfId = new CheckHelpModel().CheckCurrentUserId();
                if (feelinghelpmodel.RemoveFeelingFromDatabase(modelId, selfId))
                return Content("删除成功");
                return Content("删除失败");

            }
            catch(Exception ex)
            { 
                return Content("错误："+ex.Message);
            }
        }

        [Authorize]
       
        public ActionResult ShowFeelingList(int currentpage=1)
        {
            ViewData["PageTitle"] = "Show All Feelings";
            const int PAGESIZE = 30;
            
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            int allcount = feelinghelpmodel.GetFeelingCount(selfId);
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            ViewData["Type"] = ReturnImageHelpModel.GetFeelImage("all");
            List<FeelingModel> model = feelinghelpmodel.GetFeelingModelList(selfId, currentpage, PAGESIZE);
            return View(model);
        }

        

        [Authorize]
        [HttpPost]
        public ActionResult ShowFeelingList(string type,DateTime? minTime,DateTime? maxTime)

        {
            //在使用post方法的时候，限定了时间范围，所以没有实现分页功能;
            ViewData["PageTitle"] = "Show All Feelings";
            const int PAGESIZE = int.MaxValue;
            int currentpage = 1;
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            int allcount = 0;
           
            
            List<FeelingModel> model=new List<FeelingModel>();
            if (string.IsNullOrEmpty(type)||type=="all")
            {
                ViewData["Type"] = ReturnImageHelpModel.GetFeelImage("all");
                if (minTime == null && maxTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(selfId, currentpage, PAGESIZE);
                    allcount = feelinghelpmodel.GetFeelingCount(selfId);
                }
                else if (maxTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(selfId, currentpage, PAGESIZE, minTime ?? DateTime.MinValue);
                    allcount = feelinghelpmodel.GetFeelingCount(selfId, minTime ?? DateTime.MinValue);
                }
                else if (minTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(selfId, currentpage, PAGESIZE, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetFeelingCount(selfId, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
                else
                {
                    model = feelinghelpmodel.GetFeelingModelList(selfId, currentpage, PAGESIZE, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetFeelingCount(selfId, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }

            }
            else
            {
                ViewData["Type"] = ReturnImageHelpModel.GetFeelImage(type);
                if (minTime == null && maxTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type);
                }
                else if (maxTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, minTime ?? DateTime.MinValue);
                }
                else if (minTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
                else
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
            }


            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
           
            return View("ShowFeelingList",  model);

        }

        ////还需要添加一些获取特定表情的功能

        [Authorize]
        [HttpPost]
        public ActionResult ShowTypeFeelingList(string type, DateTime? minTime, DateTime? maxTime)
        {
            ViewData["PageTitle"] = "Show All Feelings";
            const int PAGESIZE = int.MaxValue;
            int currentpage = 1;
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            int allcount = 0;


            List<FeelingModel> model = new List<FeelingModel>();
            if (minTime == null && maxTime == null)
            {
                model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE);
                allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type);
            }
            else if (maxTime == null)
            {
                model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue);
                allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, minTime ?? DateTime.MinValue);
            }
            else if (minTime == null)
            {
                model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
            }
            else
            {
                model = feelinghelpmodel.GetTypeFeelingModelList(selfId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                allcount = feelinghelpmodel.GetTypeFeelingSomeCount(selfId, type, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
            }


            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            return View("ShowFeelingList", model);
        }



        #region  part
        public ActionResult FeelingPart()
        {
            return View();
        }
        public ActionResult SingleFeelingPart(FeelingModel model)
        {
            return View(model);
        }

        public ActionResult FeelingScriptPart()
        {
            return View();
        }
        #endregion

    }
}
